<?php
/*
*  -------------------------------------------------
*   @file		: .php
*   @link		:CPC  www.chinapet.com
*   @copyright	: 2002-2012 Xingmo Inc
*   @author		: Laipiyang <462166282@qq.com>
*   @blog_url   : http://www.icuir.com/
*   @blog_url   : http://blog.sina.com.cn/xmphp
*   @date		:
*   @update		:
*  -------------------------------------------------
*/
require "./include/init.inc.php";
$filename=basename(__FILE__);
checklogin();
$act=isset ( $_GET['act'] ) ? trim ( $_GET['act'] ) : '' ;
if($act=='add')
{
	 $smarty->assign('web',array('title'=>'通知添加','act'=>'insert','btn'=>'添加通知完成'));
	 $smarty->display(ADMIN_TPL.'notice_info.dwt');

}
else if ($act=='insert')
{


	$title=contentReplace($title);
	$content=contentReplace($content);
	$dateline=time();
	if($title=='')
	{
		alert('通知标题不可以为空!!!');
		exit();
	}
	if($content=='')
	{
	    alert('通知内容不可以为空');
		exit();
	}
	if(preg_match('/[^\x80-\xffA-Za-z0-9\w\.\-\,]/',$content))
	{
		   alert('通知内容中中含有非法字符');
		   exit();
	}
	$sql="INSERT INTO `info_q_notice` (`title`,`content`,`dateline`) VALUES ('{$title}','{$content}','{$dateline}') ";
	$res=$db->query($sql);
	if($res)
	{
		$notice_id=$db->insert_id();  //返回上一步插入的id
		if($notice_id)
		{
			 $user=$db->fetch_all("SELECT * FROM `info_link_user`");
			 $sql1="INSERT INTO `info_q_noticeids` (`notice_id`,`user_id`,`dateline`) VALUES ";
			 foreach ($user as $key => $val )
			 {
					$sql1.="('{$notice_id}','{$val['id']}','{$dateline}'),";
					//  (NULL, '', '', '', '', '', '', '夜宴'), (NULL, '', '', '', '', '', '', '夜宴'),.....
			 }
			$sql1=substr($sql1,0,-1);
			$db->query($sql1);
		}
		alert('添加通知成功!',$filename);
		exit();
	}
	else
	{
		alert('添加通知失败!',$filename);
		exit();
	}


}
else if ($act=='edit')
{
       $notice_id=isset ( $_GET['notice_id'] ) ? intval ( $_GET['notice_id'] ) : 0 ;
	   if($notice_id<1)
	   {
		 alert('操作非法');
		 exit;
	   }
	   $sql="SELECT * FROM `info_q_notice` WHERE `id`='{$notice_id}' ";
	   if(!$query=$db->fetch_one($sql))
	   {
			alert('数据不存在');
			exit;
	   }
	   $smarty->assign('web',array('title'=>'修改通知页面','act'=>'update','btn'=>'修改通知'));
	   $smarty->assign('row',$query);
	   $smarty->display('notice_info.dwt');
}
else if ($act=='update')
{
    $id=isset ( $_POST['id'] ) ? intval ( $_POST['id'] ) : 0;
	if($id<1)
	{
		alert('操作非法');
		exit;
	}
	$title=contentReplace($title);
	$content=contentReplace($content);
	$dateline=time();
	if($title=='')
	{
		alert('通知标题不可以为空!!!');
		exit();
	}
	if($content=='')
	{
	    alert('通知内容不可以为空');
		exit();
	}
	if(preg_match('/[^\x80-\xffA-Za-z0-9\w\.\-\,]/',$content))
	{
		   alert('通知内容中中含有非法字符');
		   exit();
	}
	$sql="UPDATE `info_q_notice` SET `title`='{$title}',`content`='{$content}',`dateline`='{$dateline}'  WHERE `id`='{$id}' ";
	$res=$db->query($sql);
	if($res)
	{
		alert('通知修改成功',$filename);
		exit;
	}
	else
	{
		alert('通知修改失败');
		exit;
	}

}
else if ($act=='del')
{
   $notice_id=isset ( $_GET['notice_id'] ) ? intval ( $_GET['notice_id'] ) : 0 ;
   if($notice_id<1)
   {
	 alert('操作非法');
	 exit;
   }
   //级联删除
  $sql = "DELETE m,n FROM `info_q_notice` AS m LEFT JOIN `info_q_noticeids` AS n ON m.`id`=n.`notice_id` WHERE m.`id`='{$notice_id}'";
   $res=$db->query($sql);
   if($res)
   {
	  alert('通知删除成功',$filename);
	  exit;
   }
   else
   {
	  alert('通知删除失败');
	  exit;
   }
}
else
{
	$smarty->assign('pl',$pl=licuicui_page($db->num_rows($db->query("SELECT * FROM `info_q_notice` ")),$filename,1));
	$smarty->assign('list',$db->fetch_all("SELECT * FROM `info_q_notice` ORDER BY `dateline` DESC  LIMIT {$pl['start']},{$pl['pagesize']}"));
	$smarty->assign('web',array('title'=>'通知列表'));
	$smarty->display('notice_list.dwt');

}

?>